//
//  FileTools.m
//  CommonTools
//
//  Created by xing dx on 13-1-6.
//  Copyright (c) 2013年 guowk. All rights reserved.
//

#import "FileTools.h"

static FileTools *pTools = nil;
@implementation FileTools

#pragma mark -
#pragma mark ==========================================
#pragma mark init
+ (id)getInstance {
  @synchronized(self) {
		if (pTools == nil) {
			pTools = [[FileTools alloc] init];
		}
	}
  
  return pTools;
}

#pragma mark -
#pragma mark ==========================================
#pragma mark 基础功能

- (BOOL)isExistAtPath:(NSString *)filePath {
  BOOL success;
  
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//检查目录文件
	success = [fileManager fileExistsAtPath:filePath];
  
  return success;
}

- (BOOL)createFileAtPath:(NSString *)filePath {
  BOOL success;
  
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//检查目录文件
	success = [fileManager fileExistsAtPath:filePath];
  
  if (!success) {
    success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
  }
  else {
    success = YES;
  }
  return success;
}

- (BOOL)createFolderAtPath:(NSString *)folderPath {
  BOOL success;
  
	NSFileManager *fileManager = [NSFileManager defaultManager];
	//检查目录文件
	success = [fileManager fileExistsAtPath:folderPath];
  
  if (!success) {
    success = [fileManager createFileAtPath:folderPath contents:nil attributes:nil];
  }
  else {
    success = YES;
  }
  return success;
}

- (BOOL)copyFileIfNeeded:(NSString *)fileName {
  BOOL success;
	NSFileManager *fileManager = [NSFileManager defaultManager];
	NSError *error;
	
	//检查、拷贝书籍目录文件
	NSString *writablePath = [_Document stringByAppendingPathComponent:fileName];
	success = [fileManager fileExistsAtPath:writablePath];
	if(!success){
		NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileName];
		success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
		if (!success) {
			success = [fileManager createFileAtPath:writablePath contents:nil attributes:nil];
		}
	}
  return success;
}

@end
